//
//  FunyPicDetailsViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/5/10.
//

#import "FunyPicDetailsViewController.h"
#import <YBPopupMenu/YBPopupMenu.h>
#import "MapDetailTableViewCell.h"
#import "FunPicTableViewCell.h"
#import "ReportViewController.h"
@interface FunyPicDetailsViewController ()<UITableViewDelegate,UITableViewDataSource,YBPopupMenuDelegate>
@property (nonatomic , strong) SQCustomButton * loveBtn;
@property (nonatomic , strong) SQCustomButton * outBtn;

@end

@implementation FunyPicDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"详情");
    WS(weakSelf);
    [self zx_setRightBtnWithImg:SQGetImage(@"shanchu") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
       NSArray * array = @[@"举报",@"拉黑"];
        NSArray * icon = @[@"jubao",@"lahei"];
        if ([self.model.uid isEqualToString:[UserModel share].userid]) {
            array = @[@"删除"];
            icon = @[@"lajitong"];
        }
       YBPopupMenu * pop =  [YBPopupMenu showRelyOnView:btn titles:array icons:icon menuWidth:120 otherSettings:^(YBPopupMenu *popupMenu) {
            
        }];
        pop.delegate = weakSelf;
    }];
}
- (void)SQ_UIViewForSelfView{
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.zx_navBar.mas_bottom);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    [self SQ_tableViewAddRefresh];
    [self SQ_Header];
    [self.view addSubview:self.outBtn];
    [self.outBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(50);
            make.right.equalTo(self.view.mas_right).offset(-6);
            make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-50);
    }];
    [self.view addSubview:self.loveBtn];
    [self.loveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(50);
            make.right.equalTo(self.view.mas_right).offset(-6);
            make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-130);
    }];
    WS(weakSelf);
    [self.loveBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_loveBtn];
    }];
    [self.outBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_outBtn];
    }];
}
- (void)SQ_Header{
    [self SQ_topView];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"1" forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [dic setObject:self.model.userid forKey:@"pid"];
    [HttpTool post:Url47 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page = 1;
        [self.tableView.mj_header endRefreshing];
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [PointModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray removeAllObjects];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_header endRefreshing];
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
- (void)SQ_Foother{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSString stringWithFormat:@"%d",self.page+1] forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [dic setObject:self.model.userid forKey:@"pid"];
    [HttpTool post:Url47 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page ++;
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [PointModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}
-(void)SQ_topView{
    NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
    [dict setObject:self.model.userid forKey:@"id"];
     [HttpTool post:Url40 paramDic:dict successBlock:^(BaseModel * _Nonnull httpModel) {
         FunyModel * model = [FunyModel mj_objectWithKeyValues:httpModel.data];
         self.model = model;
         [self.tableView reloadData];
         if (self.model.isLike) {
             self.loveBtn.image = SQGetImage(@"xihuan-1-2");
         } else {
             self.loveBtn.image = SQGetImage(@"xihuan-1");
         }
         if (self.model.isNoLike) {
             self.outBtn.image = SQGetImage(@"buxihuan-2");
         } else {
             self.outBtn.image = SQGetImage(@"buxihuan");
         }
     } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
         [YJProgressHUD showMsgWithoutView:httpModel.message];
     }];
}
- (void)ybPopupMenu:(YBPopupMenu *)ybPopupMenu didSelectedAtIndex:(NSInteger)index{
    if (index ==0) {
        if ([self.model.uid isEqualToString:[UserModel share].userid]) {
            //删除
            NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
            [dic setObject:self.model.userid forKey:@"id"];
            [HttpTool post:Url44 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
                if (self.isRemoveBlock) {
                    self.isRemoveBlock(self.model.userid);
                }
                [self SQ_popViewVC];
            } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
                [YJProgressHUD showMsgWithoutView:httpModel.message];
            }];
        }else{
            ReportViewController * vc = [[ReportViewController alloc]init];
            vc.userid = self.model.userid;
            vc.type = @"1";
            [self.navigationController pushViewController:vc animated:YES];
        }
    } else {
        NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
        [dic setObject:self.model.uid forKey:@"pid"];
        [dic setObject:@"1" forKey:@"type"];
        [HttpTool post:Url42 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
            if (self.isBlackRemoveBlock) {
                self.isBlackRemoveBlock(self.model.uid);
            }
            [self SQ_popViewVC];
        } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
            [YJProgressHUD showMsgWithoutView:httpModel.message];
        }];
    }
}
-(void)SQ_loveBtn{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:self.model.userid forKey:@"id"];
    if (self.model.isLike) {
        [dic setObject:@"0" forKey:@"type"];
    } else {
        [dic setObject:@"1" forKey:@"type"];
    }
    [HttpTool post:Url41 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [self SQ_Header];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_outBtn{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:self.model.userid forKey:@"id"];
    if (self.model.isNoLike) {
        [dic setObject:@"2" forKey:@"type"];
    } else {
        [dic setObject:@"3" forKey:@"type"];
    }
    [HttpTool post:Url41 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [self SQ_Header];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
  

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 1;
    } 
     return self.jsonArray.count;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        FunPicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FunPicTableViewCell"];
        if (!cell) {
            cell = [[FunPicTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"FunPicTableViewCell"];
        }
        cell.isDetaile = YES;
        cell.model = self.model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    MapDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MapDetailTableViewCell"];
    if (!cell) {
        cell = [[MapDetailTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"MapDetailTableViewCell"];
    }
    cell.model = [self.jsonArray objectAtIndex:indexPath.item];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return [FunPicTableViewCell FSCellHeightForTableView:tableView indexPath:indexPath cellContentViewWidth:0 bottomOffset:10];
    }
    return  70;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return;
    }
    PointModel * model = [self.jsonArray objectAtIndex:indexPath.item];
    PeopleDetailViewController * vc = [[PeopleDetailViewController alloc]init];
    vc.uid = model.uid;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 0.01;
    }
    return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return [[UIView alloc]init];
    }
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 44)];
    view.backgroundColor = UIColor.clearColor;
    UILabel * number = [[UILabel alloc]init];
    number.text = [NSString stringWithFormat:@"%@ %d",SQNSLocalString(@"点赞"),self.model.likeNum];
    number.textColor = UIColor.whiteColor;
    number.font = FONT_MEDIUM(16);
    [view addSubview:number];
    [number mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.left.equalTo(view).offset(12);
    }];
    return view;
}

- (SQCustomButton *)loveBtn{
    if (!_loveBtn) {
        _loveBtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(30, 30) midmargin:0];
        _loveBtn.image = SQGetImage(@"xihuan-1");
        _loveBtn.backgroundColor = SQHexColor(@"#FFFFFF", 0.4);
        _loveBtn.layer.cornerRadius = 25;
    }
    return _loveBtn;
}
 
- (SQCustomButton *)outBtn{
    if (!_outBtn) {
        _outBtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(30, 30) midmargin:0];
        _outBtn.image = SQGetImage(@"buxihuan");
        _outBtn.backgroundColor = SQHexColor(@"#FFFFFF", 0.4);
        _outBtn.layer.cornerRadius = 25;
    }
    return _outBtn;
}
@end
